var mongoose = require('mongoose');
var Product = require('../models/product.model')// 引数据模型
var path=require('path');

// 新增
exports.create = function(req,res,next){
    var product = new Product(req.body); // 从req.body里获取参数对象，比如{name:'vane',gender:'男',city:'杭州'....}
    product.save()
    .then(data=>{
        res.json(data)
    })
}

exports.getone = function(req,res,next){
    var id = req.params.id;

    Product.findById(id,function(err,result){
        console.log(result);
        res.json(result)
    })
    
}

exports.getcate = function(req,res,next){
    var page = (req.body.page) ? req.body.page : 1;
    var limit = (req.body.limit) ? req.body.limit : 9;

    var queryCondition = {};
    var postId;
    if( req.body.postId && req.body.postId.trim().length>0 ){
        postId = req.body.postId;
        queryCondition = {"cateId":postId}
    }
    Product.paginate(queryCondition, { page: page, limit: limit }, function(err, result) {
        result.rows = result.docs;
        delete result.docs;
        res.json(result)
    });

}

//修改
exports.update = function(req,res,next){
    var id = req.params.id; // req.param('id')
    // 现在操作的都是mongoose而不是mongodb
    Product.findByIdAndUpdate(id,{$set: req.body}, {new:false} )
    .then(data=>{
        res.json({msg:'数据修改成功',status:200}); // 如果你要返回数据，这个data返回的是你之前修改的那个数据内容
    })
}

//删除
exports.remove = function(req,res,next){
    var id = req.params.id;
    Product.findByIdAndRemove(id,function(err,doc){
        res.json({msg:'数据删除成功',status:200})
    })
} 

exports.removes = function(req,res,next){
    var ids = req.body.ids;
    Product.remove({ _id:{ $in: ids }})
    .then(data=>{
        res.json({msg:'数据删除成功',status:200})
    })
} 

exports.search = function(req,res,next){
    var page = (req.body.page) ? req.body.page : 1;
    var limit = (req.body.limit) ? req.body.limit : 2;

    var queryCondition = {};
    var productname,cateId;
    if( req.body.productname && req.body.productname.trim().length>0 && req.body.cateId==null){
        productname = req.body.productname;
        queryCondition = {"productname":new RegExp(productname,'i')}
    }else if(req.body.productname && req.body.productname.trim().length>0 && req.body.cateId!==null){
        productname = req.body.productname;
        cateId = req.body.cateId;
        queryCondition = {"productname":new RegExp(productname,'i'),cateId:cateId}
    }


    Product.paginate(queryCondition, { page: page, limit: limit }, function(err, result) {
        result.rows = result.docs;
        delete result.docs;
        res.json(result)
    });
}

exports.classifyLists = function (req, res, next) {
    var ids = req.body.cateId.split(",");
    if (ids.length > 0) {
        DataModel.find({cateId: {$in: ids}}).then(data=> {
                res.json(data);
            })
    } else {
        res.json({"msg": "请求失败", "status": 404})
    }
}


exports.list = function(req,res,next){
    var page = (req.body.page) ? req.body.page : 1;
    var limit = (req.body.limit) ? req.body.limit : 9;
    //定义查询条件
    var queryCondition = {};
    var cateId;
    if( req.body.title && req.body.title.trim().length>0 ){
        cateId = req.body.cateId;
        queryCondition = {
            "cateId":new RegExp(cateId,'t')
            
        }
    }
    if( req.body.title && req.body.title.trim().length>0 ){
        title=req.body.title;
        queryCondition = Object.assign(queryCondition,
			{
				title: title
			}
		)
    }


    Product.paginate(queryCondition, { page: page, limit: limit }, function(err, result) {
        result.rows = result.docs;
        delete result.docs;
       res.json(result)
    });
}

function reverseTree(data,pid){
    var result=[],temp;

    var data= JSON.parse(JSON.stringify(data));

    for(var i in data){
        if(data[i].parentId === pid){
            result.push(data[i]);
            temp = reverseTree(data,data[i]._id);
            if(temp.length>0){
                data[i].children = temp;
            }
        }
    }
    return result;
}